make_texreg <- function(name, model, custom.model.names = NULL, custom.coef.map = NULL, custom.header = NULL, 
                        custom.note = NULL, custom.gof.rows = NULL, resize.width = FALSE, caption = NULL, scalebox = 1,
                        override.se = 0, override.pvalues = 0, reorder.gof = NULL, include.rsquared = TRUE,
                        stars = c(0.001, 0.01, 0.05), multiple.tasks = "clustered", refresh = T){
  addspacing <- function(table){
    x <- paste0("\\renewcommand{\\baselinestretch}{1.25}%\n", table)
    y <- paste0(x, "\n\\renewcommand{\\baselinestretch}{1.67}%")
    z <- str_replace(y, "\\n\\\\usepackage\\{threeparttable\\}\\n", "")
    return(z)
  }
  
  if(multiple.tasks == "clustered"){
    custom.gof.names = c('R$^2$', 'Observations', 'Respondents')
    # include.nclusts = include.nclusts
  }
  else if(multiple.tasks == "normal") {
    custom.gof.names = c('R$^2$', 'Observations')
  } 
  else if(multiple.tasks == "fe") {
    custom.gof.names = c('Observations', "R$^2$ (full model)", "R$^2$ (proj model)")
    #include.nclusts = include.nclusts
  }
  else if(multiple.tasks == "glm") {
    include.rsquared = FALSE
    custom.gof.names = c('Observations')
  }
  else if (multiple.tasks == "feols") {
    custom.gof.names = c('Observations', 'R$^2$')
  }
  
  table <- texreg(l = model,
                  custom.model.names = custom.model.names,
                  custom.coef.map = custom.coef.map,
                  custom.header = custom.header,
                  custom.gof.names = custom.gof.names,
                  custom.gof.rows = custom.gof.rows,
                  custom.note = custom.note,
                  caption = caption,
                  digits = 3,
                  stars = stars,
                  include.ci = FALSE,
                  caption.above = TRUE,
                  scalebox = scalebox,
                  override.se = override.se,
                  override.pvalues = override.pvalues,
                  reorder.gof = reorder.gof,
                  include.rsquared = include.rsquared,
                  include.adjrs = FALSE,
                  include.rmse = FALSE,
                  include.aic = FALSE,
                  include.bic = FALSE,
                  include.loglik = FALSE,
                  include.deviance = FALSE,
                  include.nobs = TRUE,
                  include.proj.stats = FALSE,
                  include.censnobs = FALSE,
                  include.wald = FALSE,
                  threeparttable = T,
                  label = paste0("tab:", name),
                  use.packages = F,
                  float.pos = "!t",
                  booktabs = T)
  
  if(resize.width == TRUE){
    table <- str_replace(table, paste0("\\\\scalebox\\{",scalebox,"\\}\\{"), "\\\\resizebox\\{\\\\linewidth\\}\\{\\!\\}\\{")
  }
  table <- addspacing(table)
  
  cat(table, sep = "\n")
  
  if(refresh == T){
    cat(table, sep = "\n", file = paste0(save_path, "tables/", name, ".tex"))
  }
}
